package com.jeomo.masterdata.provider.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jeomo.base.provider.controller.BaseController;
import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.dto.PageResponseDto;
import com.jeomo.common.core.request.PageRequest;
import com.jeomo.common.core.result.CommonResult;
import com.jeomo.common.core.result.ResultCode;
import com.jeomo.common.core.util.BeanCopyUtil;
import com.jeomo.common.core.vo.PageResponseVo;
import com.jeomo.masterdata.provider.request.CreateMerchantRequest;
import com.jeomo.masterdata.provider.request.UpdateMerchantRequest;
import com.jeomo.masterdata.provider.web.vo.MerchantVo;
import com.jeomo.masterdata.starter.entity.Merchant;
import com.jeomo.masterdata.starter.exception.IntegrityConstraintViolationException;
import com.jeomo.masterdata.starter.service.IGoodsService;
import com.jeomo.masterdata.starter.service.IMerchantService;
import com.jeomo.shiro.util.LoginSessionUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;


/**
 * @author 崔贺
 */
@RequestMapping("/masterData/merchant")
@RestController
public class MerchantController extends BaseController {

    @Resource
    private IMerchantService merchantService;

    @Resource
    private IGoodsService iGoodsService;

    @PostMapping
    @ApiOperation("创建商户")
    public CommonResult<String> create(@RequestBody CreateMerchantRequest merchantRequest) {
        try {
            Merchant merchant = BeanCopyUtil.copyProperties(merchantRequest, Merchant.class);

            merchantService.create(merchant);
        } catch (Exception e) {
            if (e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                throw new IntegrityConstraintViolationException();
            } else {
                e.printStackTrace();
                return CommonResult.failure("新增失败");
            }
        }
        return CommonResult.success("新增成功");

    }

    @DeleteMapping("/{code}")
    @ApiOperation("删除商户")
    public CommonResult<String> remove(@PathVariable("code") String code) {
        Boolean canMerchant = merchantService.removeMerchant(code);
        if (canMerchant) {
            return CommonResult.success("删除成功");
        } else {
            return CommonResult.failure(ResultCode.DELETE_ERROR.msg());
        }
    }

    @PutMapping
    @ApiOperation("修改商户")
    public CommonResult<String> update(@RequestBody UpdateMerchantRequest merchantRequest) {
        try {
            merchantService.updateById(BeanCopyUtil.copyProperties(merchantRequest, Merchant.class));
        } catch (Exception e) {
            if (e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                throw new IntegrityConstraintViolationException();
            } else {
                e.printStackTrace();
                return CommonResult.failure("修改失败");
            }
        }
        return CommonResult.success("修改成功");
    }

    @PostMapping("/page")
    @ApiOperation("获取商户列表")
    public CommonResult<PageResponseVo<MerchantVo>> page(@RequestBody PageRequest request) {
        PageResponseDto pageResponseDto = merchantService.pageSearch(BeanCopyUtil.copyProperties(request, PageQueryDto.class));
        return CommonResult.success(BeanCopyUtil.copyPageProperties(pageResponseDto, MerchantVo.class));
    }

    @GetMapping("/{merchantCode}")
    @ApiOperation("根据商户号查询商户实体")
    public CommonResult<MerchantVo> queryByCode(@PathVariable("merchantCode") String merchantCode) {
        Merchant merchant = merchantService.getById(merchantCode);
        return CommonResult.success(BeanCopyUtil.copyProperties(merchant, MerchantVo.class));
    }

    @GetMapping("/all")
    @ApiOperation("获取所有商户")
    public CommonResult<List<MerchantVo>> all() {
        QueryWrapper<Merchant> wrapper = new QueryWrapper<>();
        wrapper.eq("org_code", LoginSessionUtil.getCurrentLoginInfo().getCurrentOrgCode());
        wrapper.eq("start_flag", true);
        List<Merchant> merchants = merchantService.list(wrapper);
        return CommonResult.success(BeanCopyUtil.copyListProperties(merchants, MerchantVo::new));
    }

    @PostMapping("/allMerchant")
    @ApiOperation("获取所有商户")
    public CommonResult<List<MerchantVo>> allMerchant(@RequestBody PageRequest request) {
        PageResponseDto pageResponseDto = merchantService.pageSearch(BeanCopyUtil.copyProperties(request, PageQueryDto.class));
        List items = pageResponseDto.getItems();
        return CommonResult.success(items);
    }

    @PutMapping("/updateStartFlag")
    public CommonResult<String> updateStartFlag(@RequestBody UpdateMerchantRequest updateMerchantRequest) {
        Merchant merchant = BeanCopyUtil.copyProperties(updateMerchantRequest, Merchant.class);
        int i = merchantService.updateMerchantAndGoodsFlag(merchant.getCode(), merchant.getStartFlag());
        return i > 1 ? CommonResult.success("修改成功") : CommonResult.failure("修改失败");
    }
}
